import {
  classicBeh
} from "../classic-beh.js";

const mMgr = wx.getBackgroundAudioManager();

Component({

  behaviors: [classicBeh],
  /**
   * 组件的属性列表
   */
  properties: {
    musicUrl: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    playing: false,
    pauseSrc: "./images/player@pause.png",
    playSrc: "./images/player@play.png"
  },

  attached: function() {
    this._recoverStatus();
    this._monitorSwitch();
  },

  /**
   * 组件的方法列表
   */
  methods: {

    onPlay: function(event) {
      this.setData({
        playing: !this.data.playing,
      })
      if (this.data.playing) {
        mMgr.src = this.properties.musicUrl;
        mMgr.title = "音乐";
      } else {
        mMgr.pause();
      }
    },

    _recoverStatus: function() {
      let playing = false;
      if (mMgr.paused) {
        playing = false;
      } else if (mMgr.src == this.properties.musicUrl) {
        playing = true;
      }
      this.setData({
        playing: playing
      })
    },

    _monitorSwitch: function() {
      mMgr.onPlay(() => {
        this._recoverStatus();
      });
      mMgr.onPause(() => {
        this._recoverStatus();
      });
      mMgr.onStop(() => {
        this._recoverStatus();
      });
      mMgr.onEnded(() => {
        this._recoverStatus();
      });
    },

  }
})